package com.ybkj.daijia.server.event.listener;

import com.ybkj.daijia.serverUtils.SettingUtils;
import com.ybkj.daijia.server.event.model.OrderFillV2Event;
import com.ybkj.daijia.server.model.Company;
import com.ybkj.daijia.server.model.Driver;
import com.ybkj.daijia.server.model.Order;
import com.ybkj.daijia.server.model.SmsTemplet;
import com.ybkj.daijia.server.model.SmsTemplet.SmsType;
import com.ybkj.daijia.server.service.CompanyService;
import com.ybkj.daijia.server.service.DriverService;
import com.ybkj.daijia.server.service.MemberService;
import com.ybkj.daijia.server.service.SmsService;
import com.ybkj.daijia.server.service.SmsTempletService;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

@Component
public class OrderFillV2EventListener implements ApplicationListener<OrderFillV2Event> {

    private static final Logger logger = LoggerFactory.getLogger(OrderFillV2EventListener.class);

    @Autowired
    private SmsTempletService smsTempletService;
    @Autowired
    private SmsService smsService;
    @Autowired
    private DriverService driverService;

    @Autowired
    private MemberService memberService;

    @Autowired
    private SettingUtils settingUtils;

    @Autowired
    private CompanyService companyService;


    @Override
    @Async
    public void onApplicationEvent(OrderFillV2Event event) {

        Order order = (Order) event.getSource();
        logger.debug("order fillv2 event:{}", order.getOrderNumber());

        Driver driver = driverService.findOne(order.getDriverId());

        Company mainCompany = companyService.getMain();
        Company orderCompany = companyService.findOne(order.getCompanyId());

        //推送数据到浏览器
//		JSONObject jsonObject = new JSONObject();
//		jsonObject.put("driverID", driver.getId());
//		jsonObject.put("isworkCar", driver.isWorkCar());
//		jsonObject.put("type", "fillorderv2");
//		WebSocketSend.boardcastToArea(jsonObject, orderCompany.getCityName(),mainCompany.getId());

        //发送短信
        if (settingUtils.get().isSmsMainCompany()) {
            orderCompany = mainCompany;
        }
        SmsTemplet templet = smsService.findByAlias(orderCompany.getId(), SmsType.driverAccept);
        if (null != templet && StringUtils.isNotBlank(templet.getContent())) {
            try {
                smsService.sendSms(order.getClientPhone(), "driverAccept",
                    "{\"服务人员名\":\"" + driver.getName() + "\",\"服务人员工号\":\"" + driver.getUsername()
                        + "\",\"服务人员电话\":\"" + driver.getPhone() + "\",\"订单号\":\"" + "***" + order
                        .getOrderNumber().substring(8) + "\"}", orderCompany.getId(), 0,
                    orderCompany.getSignName());
            } catch (Exception e) {
                logger.error("发送短信发生异常：{}", e.getMessage());
            }
        }

    }

}
